//
// VTDUpcomingItem.m
//
// Copyright (c) 2014 Mutual Mobile (http://www.mutualmobile.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "VTDUpcomingItem.h"
#import "NSCalendar+CalendarAdditions.h"


@interface VTDUpcomingItem()

@property (nonatomic, assign) VTDNearTermDateRelation dateRelation;
@property (nonatomic, strong) NSDate*                 dueDate;
@property (nonatomic, copy)   NSString*               title;

@end


@implementation VTDUpcomingItem

+ (instancetype)upcomingItemWithDateRelation:(VTDNearTermDateRelation)dateRelation dueDate:(NSDate *)dueDate title:(NSString *)title
{
    VTDUpcomingItem *item = [[VTDUpcomingItem alloc] init];
    
    item.dateRelation = dateRelation;
    item.dueDate = dueDate;
    item.title = title;
    
    return item;
}


- (NSString *)description
{
    return [NSString stringWithFormat:@"%@    %ld  %@  %@", [super description], (long)self.dateRelation, [NSDateFormatter localizedStringFromDate:self.dueDate dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle], self.title];
}


- (BOOL)isEqualToUpcomingItem:(VTDUpcomingItem *)item
{
    if (!item)
    {
        return NO;
    }
    
    BOOL hasEqualRelations = (self.dateRelation == item.dateRelation);
    BOOL hasEqualDueDates = (!self.dueDate && !item.dueDate) || [[NSCalendar autoupdatingCurrentCalendar] isDate:self.dueDate equalToYearMonthDay:item.dueDate];
    BOOL hasEqualTitles = (!self.title && !item.title) || [self.title isEqualToString:item.title];
    
    return hasEqualRelations && hasEqualDueDates && hasEqualTitles;
}


- (BOOL)isEqual:(id)object
{
    if (self == object)
    {
        return YES;
    }
    
    if (![object isKindOfClass:[VTDUpcomingItem class]])
    {
        return NO;
    }
    
    return [self isEqualToUpcomingItem:object];
}


- (NSUInteger)hash
{
    return self.dateRelation ^ [self.dueDate hash] ^ [self.title hash];
}

@end
